package edu.scu.declaresys.web.action;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import edu.scu.declaresys.config.IAttrName;
import edu.scu.declaresys.dao.util.Depth;
import edu.scu.declaresys.service.ServiceFactory;
import edu.scu.declaresys.service.intr.IProjectService;
import edu.scu.declaresys.util.Text;
import edu.scu.declaresys.vo.Project;
import edu.scu.declaresys.web.ABaseAction;

public class GeneralProjectAction extends ABaseAction implements
		ModelDriven<Project>
{
	private static final long	serialVersionUID	= 160980125743448898L;
	
	private IProjectService projectService = ServiceFactory.createProjectService();
	
	private Project project = new Project();
	
	//
	// General project actions.
	//
	/**
	 * 查看项目申报信息
	 *@return
	 *@throws Exception
	 */
	public String generalViewProject() throws Exception
	{
		Integer id = (project.getId() == null ? (Integer)request.getAttribute(Project.T_ID) : project.getId());
		if(id == null)
		{
			throw new Exception("未指定项目");
		}
		
		project = projectService.findById(id, Depth.ONE);
		
		if(project == null)
		{
			throw new Exception("指定项目不存在");
		}
		request.setAttribute(IAttrName.PROJECT_VO, project);
		return Action.SUCCESS;
	}
	
	/**
	 * 修改项目申报信息
	 *@return
	 *@throws Exception
	 */
	public String generalDoModifyProject() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定项目");
		}
		Project dbProject = projectService.findById(project.getId(), Depth.ONE);
		if(dbProject == null)
		{
			throw new Exception("指定项目不存在");
		}
		//
		int state = dbProject.getState();
		if(state == Project.STATE_REQUEST_MODIFY
				|| state == Project.STATE_UNAUDIT_UNMODIFIABLE
				|| state == Project.STATE_PASSED
				|| state == Project.STATE_REJECTED)
		{
			throw new Exception("当前项目不可以修改");
		}
		
		/**
		 * 设置修改的值，检查可修改列是否合法并
		 */
		Project newProject = new Project();
		newProject.setId(project.getId());
		// project class id.
		if(project.getProjectClassId() != null)
		{
			if(ServiceFactory.createProjectClassService().findClass(project.getProjectClassId()) == null)
			{
				throw new Exception("项目类别不存在");
			}
			newProject.setProjectClassId(project.getProjectClassId());
		}
		// name.
		String name = project.getName();
		if(!Text.isNull(name))
		{
			if(name.isEmpty())
			{
				throw new Exception("项目名不能为空");
			}
			Project tmp = null;
			if((tmp = projectService.findByNameUnique(name)) != null
					&& !tmp.getId().equals(project.getId()) )
			{
				throw new Exception("项目名重复");
			}
			newProject.setName(name);
		}
		// Check empty.
		checkEmpty(project.getProjectFileno(), "项目文件号");
		checkEmpty(project.getLegalMan(), "项目法人");
		checkEmpty(project.getTechLeader(), "项目负责人");
		checkEmpty(project.getIntro(), "项目简介");
		newProject.setProjectFileno(project.getProjectFileno());
		newProject.setLegalMan(project.getLegalMan());
		newProject.setTechLeader(project.getTechLeader());
		newProject.setIntro(project.getIntro());
		// check and set funds.
		Integer fundType = project.getFundType();
		if(fundType != null && !fundType.equals(Project.FUND_THREE_CITY)
				&& !fundType.equals(Project.FUND_UNDEVOLOPED_AREA))
		{
			throw new Exception("项目资金类别不存在");
		}
		newProject.setFundType(fundType);
		Long grantFunds = (project.getGrantFunds() == null ? dbProject.getGrantFunds() : project.getGrantFunds());
		Long otherFunds = (project.getOtherFunds() == null ? dbProject.getOtherFunds() : project.getOtherFunds());
		Long selfFunds = (project.getSelfRaisedFunds() == null ? dbProject.getSelfRaisedFunds() : project.getSelfRaisedFunds());
		if(grantFunds < 0 || otherFunds < 0 || selfFunds < 0)
		{
			throw new Exception("资金不能为负");
		}
		newProject.setGrantFunds(grantFunds);
		newProject.setOtherFunds(otherFunds);
		newProject.setSelfRaisedFunds(selfFunds);
		newProject.setTotalFunds(grantFunds + otherFunds + selfFunds);
		//
		projectService.update(newProject);
		//
		request.setAttribute(IAttrName.PROJECT_VO, projectService.findById(project.getId(), Depth.ONE));
		request.setAttribute(IAttrName.CLASS_VO, ServiceFactory.createProjectClassService().findClassList());
		return Action.SUCCESS;
	}
	
	
	public String generalShowProjectForModify() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定项目");
		}
		Project dbProject = projectService.findById(project.getId(), Depth.ONE);
		if(dbProject == null)
		{
			throw new Exception("指定项目不存在");
		}
		//
		int state = dbProject.getState();
		if(state == Project.STATE_REQUEST_MODIFY
				|| state == Project.STATE_UNAUDIT_UNMODIFIABLE
				|| state == Project.STATE_PASSED
				|| state == Project.STATE_REJECTED)
		{
			throw new Exception("当前项目不可以修改");
		}
		//
		request.setAttribute(IAttrName.PROJECT_VO, dbProject);
		request.setAttribute(IAttrName.CLASS_LIST, ServiceFactory.createProjectClassService().findClassList());
		return Action.SUCCESS;
	}
	
	//
	// help function
	//
	private void checkEmpty(String str, String item) throws Exception
	{
		if(str != null && str.isEmpty())
		{
			throw new Exception(item + "不能为空");
		}
		return ;
	}
	
	//
	// Getters and Setters
	//
	@Override
	public Project getModel()
	{
		return project;
	}


	public Project getProject()
	{
		return project;
	}


	public void setProject(Project project)
	{
		this.project = project;
	}
	
}
